package com.zjsy.service.impl;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zjsy.dao.MagazineDao;
import com.zjsy.dao.MainNewsDao;
import com.zjsy.page.PageBean;
import com.zjsy.page.PageConstants;
import com.zjsy.po.Magazine;
import com.zjsy.po.MagazineNewList;
import com.zjsy.po.MagazineNews;
import com.zjsy.po.MainNewsList;
import com.zjsy.service.MagazineService;
@Service
@Transactional
public class MagazineServiceImpl implements MagazineService {
	
	@Autowired
	private MagazineDao magazinedao;

	@Override
	public void addMagazine(Magazine magazine) {
		
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//		magazine.setDate(df.format(new Date()));// new Date()为获取当前系统时间
		 magazinedao.addMagazine(magazine);

	}

	@Override
	public List<Magazine> findMagazine() {
		// TODO Auto-generated method stub
		return magazinedao.findMagazine();
	}

	@Override
	public PageBean<Magazine> findMagazineByPage(int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;
		int min = (pc-1)*ps;
		int max = ps;
		//得到beanList
		List<Magazine> beanLsit = magazinedao.findMagazineByPage(min, max);
		for(int i=0;i<beanLsit.size();i++) {
			Magazine magazine = beanLsit.get(i);
			
			if(magazine.getImgurl()!=null&&magazine.getImgurl().contains("image"))
			magazine.setImgurl(magazine.getImgurl().substring(magazine.getImgurl().indexOf("im"), magazine.getImgurl().length()));
		}
		int tr = magazinedao.findMagazine().size();
		PageBean<Magazine> pb = new PageBean<>();
		
		
	
		pb.setBeanList(beanLsit);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}

	@Override
	public Magazine findMagazineById(String id) {
		
		Magazine magazine=magazinedao.findMagazineById(id);
		if(magazine.getImgurl()!=null&&magazine.getImgurl().contains("image"))
		magazine.setImgurl(magazine.getImgurl().substring(magazine.getImgurl().indexOf("im"), magazine.getImgurl().length()));
		return magazine;
	}

	@Override
	public void updateMagezine(Magazine magazine) {
		
		magazinedao.updateMagezine(magazine);
		
		
	}

	@Override
	public void deleteMagazineById(String id) {
		
		magazinedao.deleteMagazineById(id);
		
	}

	@Override
	public List<MagazineNewList> findMagazineNew() {
		// TODO Auto-generated method stub
		return magazinedao.findMagazineNew();
	}

	@Override
	public PageBean<MagazineNewList> findMagazineNewByPage(int pc) {
		int ps = PageConstants.BOOK_PAGE_SIZE;
		int min = (pc-1)*ps;
		int max = ps;
		//得到beanList
		List<MagazineNewList> beanLsit = magazinedao.findMagazineNewByPage(min, max);
		int tr = magazinedao.findMagazineNew().size();
		PageBean<MagazineNewList> pb = new PageBean<>();
		pb.setBeanList(beanLsit);
		pb.setPc(pc);
		pb.setPs(ps);
		pb.setTr(tr);
		return pb;
	}

	@Override
	public void addMagazineNews(MagazineNews magazinenews) {
		
		String  name=magazinenews.getFeatureName();
		String feature=magazinedao.findFeatureByName(name);
		if(feature!=null) {
			magazinenews.setFeature(feature);
		}
		else {
			magazinedao.addFeature(name);
			String newfeature=magazinedao.findFeatureByName(name);
			magazinenews.setFeature(newfeature);
		}
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		magazinenews.setUptime(df.format(new Date()));// new Date()为获取当前系统时间
		String periods=magazinedao.findIdByperiods(magazinenews.getPeriodical());
		if(periods==null)
		periods="0";
		magazinenews.setPeriodical(periods);
		magazinedao.addMagazineNews(magazinenews);
 
		
	}

	@Override
	public MagazineNews findMagazineNewsById(String id) {
		
		MagazineNews magazinenews=magazinedao.findMagazineNewsById(id);
		magazinenews.setFeatureName(magazinedao.findFeatureNameById(magazinenews.getFeature()));
		Magazine magazine=magazinedao.findperiodsById(magazinenews.getPeriodical());
		if(magazine!=null)
		magazinenews.setPeriodical(magazine.getPeriods());
		else
		magazinenews.setPeriodical(null);
		
		return magazinenews;
	}

	@Override
	public void updateMagazineNews(MagazineNews magazinenews) {
		
		String featureName=magazinenews.getFeatureName();
		String feature=magazinedao.findFeatureByName(featureName);
		if(feature!=null) {
			magazinenews.setFeature(feature);
		}
		else {
			magazinedao.addFeature(featureName);
			String newfeature=magazinedao.findFeatureByName(featureName);
			magazinenews.setFeature(newfeature);
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		magazinenews.setUptime(df.format(new Date()));// new Date()为获取当前系统时间
		
		String periods=magazinedao.findIdByperiods(magazinenews.getPeriodical());
		if(periods==null)
		periods="0";
		magazinenews.setPeriodical(periods);
		
		magazinedao.updateMagazineNews(magazinenews);
		
		
	}

	@Override
	public void deleteMagazineNewsById(String id) {
		magazinedao.deleteMagazineNewsById(id);
	}

}
